//
//  ViewController.swift
//  YD_PhotoPicker_Swift3
//
//  Created by alyadan on 2016/11/26.
//  Copyright © 2016年 alyadan. All rights reserved.
//

import UIKit
import MVVM_BaseViewController
import YD_Util
import RxSwift
import RxCocoa
import YD_PhotoPicker_Framework

class ViewController: MVVM_BaseViewController {
    
    @IBOutlet weak var isAlbumSwitch: UISwitch!
    
    @IBOutlet weak var isPhotoSwitch: UISwitch!
    
    @IBOutlet weak var showImageView: UIImageView!
    override var currentModel: HomeVCModel{
       return self.vcModel.value as! HomeVCModel
    }
    
    
    override func viewDidLoad() {
        self.vcModel.value = HomeVCModel(parentViewModel: nil)
        super.viewDidLoad()
        
    }
    
    override func setupUI() {
        super.setupUI()
        currentModel.switchValueChange(isPhoto: isPhotoSwitch.isOn, isCamera: isAlbumSwitch.isOn)
    }
    
    override func bindModelData() {
        currentModel.selectImage.asObservable()
            .bind { [unowned self](x: UIImage?) in
           self.showImageView.image = x
        }
            .disposed(by: disposeBag)
    }
    
    @IBAction func switchValueChange(_ sender: Any) {
        currentModel.switchValueChange(isPhoto: isPhotoSwitch.isOn, isCamera: isAlbumSwitch.isOn)
    }
    
    @IBAction func change1(_ sender: Any) {
        switchValueChange(sender)
    }
    
    @IBAction func change2(_ sender: Any) {
        switchValueChange(sender)
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
       return .lightContent
    }
//    override func preferredStatusBarStyle() -> UIStatusBarStyle {
//        return .LightContent
//    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    @IBAction func selectPhoto(_ sender: Any) {
        currentModel.selectPhoto()
    }
    
}

import Photos

class HomeVCModel: MVVM_BaseVCModel {
    
    var selectImage = Variable<UIImage?>(nil)
    
    var tool:PhotoAndVideoSelectTool = {
        let x = PhotoAndVideoSelectTool()
        x.setup(type: PhotoAndVideoSelctType.AlbumPhoto, maxCount: 4, neetCut: true, orignalData: true,complete:nil)
        x.shouldShowAlbumList = false
        return x
        
    }()
    
    override func setupModel() {
        tool.completeCallBack = { [weak self](result:AnyObject?) in
            if let image = result as? [UIImage], ((result as? [UIImage])?.count)! >= 1{
                self?.selectImage.value = image.first
                printLog("result -- \(result)")
            }
        }
    }
    //
        func selectPhoto()  {
            tool.startPick(viewController: self.MVVM_modelController!)
        }
        
        func switchValueChange(isPhoto:Bool, isCamera:Bool) {
            if !isCamera{
                tool.selectType = isPhoto ? PhotoAndVideoSelctType.CameraPhoto : PhotoAndVideoSelctType.CameraVideo
            }else{
                tool.selectType = isPhoto ? PhotoAndVideoSelctType.AlbumPhoto : PhotoAndVideoSelctType.AlbumVideo
            }
        }
}
